import { Application } from 'egg';
import {
  indexRule,
  createRule,
  updateRule,
  destroyRule,
  configRule,
  getUserRule,
  setUserRule
 } from '../validate/shop/list';

/**
 * 商家管理
 */
export default (app: Application) => {
  const { auth, header } = app.middleware;
  const {  jwt } = app;

  return {
    'shop.list': {
      method: 'GET',
      path: '/api/shop/list',
      options: {
        tags: ['shop/list'],
        description: '商家列表',
        handler: app.controller.shop.merchant.list,
        middlewares: [jwt, auth('shop.list')],
        validate: indexRule,
      },
    },
    'shop.create': {
      method: 'POST',
      path: '/api/shop/list',
      options: {
        tags: ['shop/create'],
        description: '创建商家',
        handler: app.controller.shop.merchant.create,
        middlewares: [jwt, auth('shop.list.create')],
        validate: createRule,
      },
    },
    'shop.update': {
      method: 'PUT',
      path: '/api/shop/list/:id',
      options: {
        tags: ['shop/update'],
        description: '更新商家',
        handler: app.controller.shop.merchant.update,
        middlewares: [jwt, auth('shop.list.update')],
        validate: updateRule,
      },
    },
    'shop.destroy': {
      method: 'DELETE',
      path: '/api/shop/list/:id',
      options: {
        tags: ['shop/delete'],
        description: '删除商家',
        handler: app.controller.shop.merchant.destroy,
        middlewares: [jwt, auth('shop.list.destroy')],
        validate: destroyRule,
      },
    },
    'shop.config': {
      method: 'PUT',
      path: '/api/shop/config',
      options: {
        tags: ['shop/config'],
        description: '商家配置修改',
        handler: app.controller.shop.merchant.shopConfig,
        middlewares: [jwt, auth('merchant.app.config')],
        validate: configRule,
      },
    },
    'shop.detail': {
      method: 'GET',
      path: '/api/shop/config',
      options: {
        tags: ['shop/config/detail'],
        description: '商家配置详情',
        handler: app.controller.shop.merchant.shopConfigDetail,
        middlewares: [jwt, auth('merchant.app.detail')],
      },
    },
    'xxm.shop.detail': {
      method: 'GET',
      path: '/xxm/shop/config',
      options: {
        tags: ['shop/config/detail'],
        description: '商家配置详情',
        handler: app.controller.shop.merchant.shopDetail,
        middlewares: [header()],
      },
    },
    'shop.users': {
      method: 'GET',
      path: '/api/shop/list/:id/users',
      options: {
        tags: ['shop/users'],
        description: '商家成员查看',
        handler: app.controller.shop.merchant.users,
        middlewares: [jwt, auth('shop.list.users')],
        validate: getUserRule,
      },
    },
    'shop.users.update': {
      method: 'PUT',
      path: '/api/shop/list/:id/users',
      options: {
        tags: ['shop/users/update'],
        description: '成员设置',
        handler: app.controller.shop.merchant.usersUpdate,
        middlewares: [jwt, auth('shop.list.usersUpdate')],
        validate: setUserRule,
      },
    },
  };
};
